#! /usr/local/bin/python3
# —*— coding: utf-8 -*-

"""
terminal helpers
"""

def color_str(data, color, bgcolor='', fixwidth=8, style=1):
    """
    \033[显示方式;字体色;背景色m ...... \033[0m

    显示方式    效果            字体色  背景色  颜色描述
    0           终端默认设置    30      40      black
    1           高亮显示        31      41      red
    2           斜体            32      42      green 
    4           使用下划线      33      43      yellow
    5           闪烁            34      44      blud
    7           反白显示        35      45      purple
    8           不可见          36      46      cyan
                                37      47      white
                                38      48      
    """

    colors = {
        'black':  30,
        'red':    31,
        'green':  32,
        'yellow': 33,
        'blue':   34,
        'purple': 35,
        'cyan':   36,
        'white':  37,
    }
    bgcolors = {
        'black':  40,
        'red':    41,
        'green':  42,
        'yellow': 43,
        'blue':   44,
        'purple': 45,
        'cyan':   46,
        'white':  47,
    }
    styles = dict(zip(range(0, 8), range(0, 8)))
    literial_styles = {
        'default': 0,
        'highlighe': 1,
        'underscore': 4,
        'blink': 5,
        'invert': 7,
        'invisible': 8,
    }
    styles = dict(styles, **literial_styles)
    color = colors.get(color, 30)
    bgcolor = bgcolors.get(bgcolor, 40)
    return '\033[{4};{0};{1}m{2:^{3}}\033[0m'.format(color, bgcolor, data, fixwidth, styles.get(style))

def refresh(row, col, data):
    """
    origin datas
    """
    print(row, col, data)

if __name__ == '__main__':
    import time
    import datetime
    import sys
    lines = [
        color_str('line one', 'green'),
        color_str('line two', 'red',),
        color_str('line three', 'yellow'),
    ]
    [ print(line) for line in lines]
    while True:
        time.sleep(1)
        i = datetime.datetime.today().second
        sys.stdout.write('\r{:>02d}'.format(i))
